package com.tfr.rabbitmq.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * @Description:
 * @ClassName: MyRabbitConfig
 * @Author: Rain
 * @Date: 2021-10-19 20:21
 * @Version: 1.0
 **/
@Configuration
@Slf4j
public class MyRabbitConfig {
    
    @Autowired
    RabbitTemplate rabbitTemplate;
    
    /**
     * @Author: Rain
     * @Description:  使用json序列化机制，进行消息转换
     * @Date: 2021/10/19 20:32
     * @Param:
     * @return: org.springframework.amqp.support.converter.MessageConverter
     **/
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
    
    @PostConstruct
    public void initTemplate(){
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                log.info(correlationData+""+b+""+s);
            }
        });
        
        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            @Override
            public void returnedMessage(ReturnedMessage returnedMessage) {
            log.info(returnedMessage.toString());
            }
        });
        
    }
}